package lib.listen;

import lib.servlet.AuthUtil;
import net.sf.json.JSONObject;
import lib.Dao.TokenDao;

import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;//SimpleDateFormat是调整输出时间的格式
import java.util.Timer;
import java.util.TimerTask;

public class GetAccessToken extends TimerTask{

    @Override
    public void run() {
        TokenDao tokenDao=new TokenDao();
        String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
                +AuthUtil.APP_ID+"&secret="+AuthUtil.APP_SECRET;
        JSONObject jsonObject;
        try {
            jsonObject = AuthUtil.doGetJson(url);
            String token=jsonObject.getString("access_token");
            System.out.println(token);
            tokenDao.settoken(token);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}